package com.lance.springbootes.test;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;

/**
 * @Classname GetData
 * @Description
 * @Date 2022/2/16 16:55
 * @Author lance
 */
public class GetData {

    public static void main(String[] args) throws IOException {

        String url = "https://search.jd.com/Search?keyword=%E8%8B%B9%E6%9E%9C13";
        Document document = Jsoup.parse(new URL(url), 40000);
        //通过ID获取到dom节点
        Element goodsListUl = document.getElementById("J_goodsList");
        Elements goodsListLi = goodsListUl.getElementsByTag("li");
        for (Element li : goodsListLi) {
            String img = li.getElementsByTag("img").eq(0).attr("data-lazy-img");
            Elements eq = li.getElementsByClass("p-img").eq(0);
            for (Element element : eq) {
                String attr = element.getElementsByTag("a").eq(0).attr("href");
                System.out.println("=======================");
                System.out.println(attr);
            }
            String name = li.getElementsByClass("p-name").eq(0).text();
            String price = li.getElementsByClass("p-price").eq(0).text();
            System.out.println("img : " + img);
            System.out.println("name : " + name);
            System.out.println("price : " + price);
        }

    }
}
